﻿using Kn.ALICE;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class FormConfig : Form
    {
        public string strUn = "220";        //额定电压
        public string strIb = "5";          //额定电流
        public string strImpConst = "1200"; //脉冲常数
        public string strRs = "300";        //锰铜阻值
        public string strPstartRadio = "0.0028";
        public string strHFconstHex = "FFFF";
        public string strKp = "";
        public string strRegPStart = "FF";

        public Form1 f1;

        public FormConfig(Form1 f1)
        {
            InitializeComponent();
            this.f1 = f1;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            DataLoad();
        }

        private void button1_Click(object sender, EventArgs e)
        {
 

            AppConfigProcess.SetConfigValue("projectCode", comboBox_projectCode.Text.Trim());
            AppConfigProcess.SetConfigValue("assetTypeCode", comboBox_assetTypeCode.Text);
            AppConfigProcess.SetConfigValue("manufacturerCode", comboBox_manufacturerCode.Text);
            AppConfigProcess.SetConfigValue("productSerialNumber", comboBox_productSerialNumber.Text);
            AppConfigProcess.SetConfigValue("HplcID", comboBox_HplcID.Text);
            AppConfigProcess.SetConfigValue("BpsDown", comboBox_BpsDown.Text);
            AppConfigProcess.SetConfigValue("HeartTime", textBox_HeartTime.Text);
            AppConfigProcess.SetConfigValue("BpsUp", comboBox_BpsUp.Text);

            AppConfigProcess.SetConfigValue("COM", comboBox_ComDown.Text);
            AppConfigProcess.SetConfigValue("COM_UP_SPLN", comboBox_ComUp.Text);

            f1.GetConfigPara();  //调用其他窗体 f1 的方法
            this.Close();
        }

        private void button_No_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public class MyItem
        {
            private string _Text = "";     //属性1 名字
            private string _Value = "";    //属性2 名字id

            public MyItem(string Text, string Value)   //构造
            {
                _Text = Text;
                _Value = Value;
            }

            public string Text { get { return this._Text; } set { this._Text = value; } }       //名称
            public string Value { get { return this._Value; } set { this._Value = value; } }    //值
        }
        void ComLoad(System.Windows.Forms.ComboBox comBox, string comKey)
        {
            foreach (string vPortName in SerialPort.GetPortNames())
            {
                MyItem comItem1 = new MyItem(vPortName, vPortName);
                comBox.Items.Add(comItem1);
            }
            comBox.DisplayMember = "Text";        //displaymember:前台显示的字段。绑定
            comBox.ValueMember = "Value";         //ValueMember:后台取值的字段。

            if (comBox.Items.Count != 0)
            {
                string strComDown = AppConfigProcess.GetConfigXml(comKey);
                int index = comBox.FindString(strComDown);
                if (index >= 0)
                {
                    comBox.SelectedIndex = index;
                }
                else
                {
                    comBox.SelectedIndex = 0;          //不存在默认选择0
                }
            }
            else
            {
                //label_ComDown.ForeColor = Color.Red;
            }
        }

        private void DataLoad()
        {
            comboBox_projectCode.Text = AppConfigProcess.GetConfigXml("projectCode");
            comboBox_assetTypeCode.Text = AppConfigProcess.GetConfigXml("assetTypeCode");
            comboBox_manufacturerCode.Text = AppConfigProcess.GetConfigXml("manufacturerCode");
            comboBox_productSerialNumber.Text = AppConfigProcess.GetConfigXml("productSerialNumber");
            comboBox_HplcID.Text = AppConfigProcess.GetConfigXml("HplcID");
            comboBox_BpsDown.Text = AppConfigProcess.GetConfigXml("BpsDown");
            textBox_HeartTime.Text = AppConfigProcess.GetConfigXml("HeartTime");
            comboBox_BpsUp.Text = AppConfigProcess.GetConfigXml("BpsUp");
            ComLoad(comboBox_ComDown, "COM");     //加载下行载波模块串口
            comboBox_ComUp.Items.Add("不启用");
            ComLoad(comboBox_ComUp, "COM_UP_SPLN");    //加载上行HDLC串口

        }

        public string CalcHFcont(string strRs, string strIb, string strUn, string strImpConst)
        {
            double dl_Un = 0.0;
            string strHFconstHex = "FFFF";
            double kVi;                              //电压回路分压系数
            kVi = 1.0 / (8 * 150 + 1);               //电压回路：电阻1k, 8个150k
            dl_Un = Convert.ToDouble(strUn);
            double dl_Vu = dl_Un * kVi;
            double dl_Ib = Convert.ToDouble(strIb);
            double dl_Rs = Convert.ToDouble(strRs);
            int iGain = 16;
            double dl_Vi = dl_Ib * (dl_Rs / 1000000) * iGain;
            double dl_ImpConst = Convert.ToDouble(strImpConst);
            double dl_HFconst = (16.1079 * dl_Vu * dl_Vi * Math.Pow(10, 11)) / (dl_ImpConst * dl_Un * dl_Ib);
            int iHFconst = Convert.ToInt32(dl_HFconst);
            strHFconstHex = iHFconst.ToString("X4");
            return (strHFconstHex);
        }

        public string CalcKp(string strImpConst, string strHFconstHex)
        {
            double dl_Kp = 3221550000000.00;
     
            double dl_ImpConst = Convert.ToDouble(strImpConst);
            double dl_HFConst = Convert.ToInt32(strHFconstHex, 16);
            dl_Kp = 3221550000000.00 / (Math.Pow(2, 32) * dl_ImpConst * dl_HFConst);
            dl_Kp *= 1000;
            dl_Kp = Math.Round(dl_Kp, 8);   //
            return (dl_Kp.ToString());
        }


        public string CalcPowerStart(string strUn, string strIb, string strKp, string strImpConst, string strRatio)
        {
            string str_Pstart = "xx";
            double dl_Pstart;
            double dl_P = Convert.ToDouble(strUn) * Convert.ToDouble(strIb) * Convert.ToDouble(strRatio);
            dl_P *= 1000;
            dl_Pstart = dl_P / Convert.ToDouble(strKp);
            double dl_RegPstart = dl_Pstart / 256;
            int iRegPstart = Convert.ToInt32(dl_RegPstart);
            str_Pstart = iRegPstart.ToString();

            string str_PstartHex = Convert.ToUInt32(str_Pstart).ToString("X4");

            return (str_PstartHex);
        }

 

        private void comboBox_projectCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox_HplcID.Text = GXCUModuleID.GetCUId(comboBox_projectCode.Text, comboBox_manufacturerCode.Text, comboBox_assetTypeCode.Text, comboBox_productSerialNumber.Text);
        }

        private void comboBox_manufacturerCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox_HplcID.Text = GXCUModuleID.GetCUId(comboBox_projectCode.Text, comboBox_manufacturerCode.Text, comboBox_assetTypeCode.Text, comboBox_productSerialNumber.Text);
        }

        private void comboBox_assetTypeCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox_HplcID.Text = GXCUModuleID.GetCUId(comboBox_projectCode.Text, comboBox_manufacturerCode.Text, comboBox_assetTypeCode.Text, comboBox_productSerialNumber.Text);
        }

        private void comboBox_manufacturerCode_TextChanged(object sender, EventArgs e)
        {

        }

        private void comboBox_productSerialNumber_TextChanged(object sender, EventArgs e)
        {
            if (comboBox_productSerialNumber.Text.Length == 14)
            {
                comboBox_HplcID.Text = GXCUModuleID.GetCUId(comboBox_projectCode.Text, comboBox_manufacturerCode.Text, comboBox_assetTypeCode.Text, comboBox_productSerialNumber.Text);
            }
        }

    }
}
